<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Plan extends Base_Controller {
    
    
    public function index() {
        $tid = $this->uri->segment(3);
        $this->data['tid'] = $tid;
        $where = [
            'tid' => $tid,
            'is_expired' => 0
        ];
        $planer = $this->getPlaner($tid);
        $limit = $this->getLimit($tid);
        $query = $this->db->select('*')->where($where)->order_by('planer_tid ASC')->limit($limit)->get('qbjh_plancode');
        $db_plancode = $query->result_array();
        if ($db_plancode) {
            foreach ($db_plancode as $k => $v) {
                $v['planer_name'] = $planer[$k];
                $plancode[$k] = $v;
            }
        }

        $this->data['plancode'] = $plancode;
        $this->data['planer'] = $planer;

        $this->load->view('public/head', $this->data);
        $this->load->view('pages/plan');
        $this->load->view('public/foot');
    }

    public function getOpencode() {
        $redata = $this->httpcode;
        $tid = $this->input->get('tid');
        $where = [
            'tid' => $tid
        ];
        $query = $this->db->select('*')->where($where)->order_by('issue DESC')->limit(25)->get('qbjh_opencode');
        $opencode = $query->result_array();
        $redata['opencode'] = $opencode;
        echo json_encode($redata);
    }

    public function details() {
        $tid = $this->uri->segment(3);
        $planer_tid = $this->uri->segment(4);
        $this->data['tid'] = $tid;
        $this->data['planer_tid'] = $planer_tid;
        $this->data['planer'] = $this->getPlaner($tid);
        $this->load->view('public/head', $this->data);
        $this->load->view('pages/details');
        $this->load->view('public/foot');
    }

   
    /*
     * 获取计划员的计划
     */
    public function getPlanerPlancode() {
        
        $tid = $this->input->get('tid');
        $planer_tid = $this->input->get('planer_tid');

        $where = [
            'tid' => $tid,
            'planer_tid'=>$planer_tid
        ];
        $query = $this->db->select('*')->where($where)->limit(10)->order_by('current_issue DESC')->get('qbjh_plancode');
        $db_plancode = $query->result_array();
        $query = $this->db->select('*')->where(['tid'=>$tid])->order_by('issue DESC')->get('qbjh_opencode');
        $last_opencode = $query->row_array();
        
        sort($db_plancode);
        $redata['opencode'] = $last_opencode;
        $redata['plancode'] = $db_plancode;
        $redata['planername'] =  $this->getPlaner($tid)[$planer_tid];
        echo json_encode($redata);
    }
    
    
    public function showPlancodeHistroy(){
        $tid = $this->uri->segment(3);
        $planer_tid = $this->uri->segment(4);
        $query_date = $this->uri->segment(5);
        isset($planer_tid)?$this->data['planer_tid']=$planer_tid:$this->data['planer_tid']=0;
        isset($planer_tid)?$this->data['query_date']=$query_date:$this->data['planer_tid']=date('Y-m-d');
        $this->data['tid'] = $tid;
        $planer = $this->getPlaner($tid);
        $this->data['planer'] = $planer;
        $this->load->view('public/head', $this->data);
        $this->load->view('pages/plancodehistory');
        $this->load->view('public/foot');
    }
    
    public function showPlancodeByDate(){
        $redata = $this->httpcode;
        $tid = $this->input->get('tid');//彩种
        $planer_tid = $this->input->get('planer_tid');
        $query_date = $this->input->get('query_date');
        $starttime = $query_date.' 00:00:00';
        $endtime = $query_date.' 23:59:59';
        
        $query = $this->db->select('*')->where("tid={$tid} AND planer_tid={$planer_tid} AND createtime>'{$starttime}' AND createtime<'{$endtime}'")
                ->order_by('id ASC')->get('qbjh_plancode');
        $plancode = $query->result_array();
        $redata['planername'] = $this->getPlaner($tid)[$planer_tid];
        $redata['plancode'] = $plancode;
        echo json_encode($redata);
    }

}
